package cn.xfdzx.android.apps.shop.presenter;

import cn.xfdzx.android.apps.shop.base.BasePresenter;
import cn.xfdzx.android.apps.shop.bean.AddAddressBeen;
import cn.xfdzx.android.apps.shop.bean.AddressListBeen;
import cn.xfdzx.android.apps.shop.bean.DeleteAddressBeen;
import cn.xfdzx.android.apps.shop.contract.AddressListContract;
import cn.xfdzx.android.apps.shop.model.AddressListModel;
import cn.xfdzx.android.apps.shop.net.RxScheduler;
import io.reactivex.functions.Consumer;

/**
 * @author
 * @date Description：
 */
public class AddressListPresenter extends BasePresenter<AddressListContract.View> implements AddressListContract.Presenter {

    private AddressListContract.Model model;

    public AddressListPresenter() {
        model = new AddressListModel();
    }


    @Override
    public void delectAddress(String id,int position) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.delectAddress(id)
                .compose(RxScheduler.<DeleteAddressBeen>Flo_io_main())
                .as(mView.<DeleteAddressBeen>bindAutoDispose())
                .subscribe(new Consumer<DeleteAddressBeen>() {
                    @Override
                    public void accept(DeleteAddressBeen bean) throws Exception {
                        mView.onDelectSuccess(bean,position);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }


    @Override
    public void initAddressData(boolean isRefresh, boolean isLoading, String page, String limit) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        if (isLoading){
            mView.showLoading();
        }
        model.initAddressData(page,limit).compose(RxScheduler.<AddressListBeen>Flo_io_main())
                .as(mView.<AddressListBeen>bindAutoDispose()).subscribe(new Consumer<AddressListBeen>() {
            @Override
            public void accept(AddressListBeen baseObjectBean) throws Exception {
                mView.onInitSuccess(baseObjectBean);
                if (isLoading){
                    mView.hideLoading();
                }
                if (isRefresh){
                    mView.finishRefresh();
                }else {
                    mView.finishLoadMore();
                }
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                mView.onError(throwable);
                if (isLoading){
                    mView.hideLoading();
                }
                if (isRefresh){
                    mView.finishRefresh();
                }else {
                    mView.finishLoadMore();
                }
            }
        });
    }

    @Override
    public void addAddres(int position,int index,String realname, String mobile, String lng, String lat, String is_default, String address_info, String house_number, String id, String label) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.addAddress(realname, mobile, lng, lat, is_default, address_info, house_number, id, label)
                .compose(RxScheduler.<AddAddressBeen>Flo_io_main())
                .as(mView.<AddAddressBeen>bindAutoDispose())
                .subscribe(new Consumer<AddAddressBeen>() {
                    @Override
                    public void accept(AddAddressBeen bean) throws Exception {
                        mView.onAddAddressSuccess(bean,position,index);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }
}
